<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label">日志时间：</label>
                                    <div class="layui-input-inline" style="width: 290px">
                                        <input name="createTime" id="pTime" class="layui-input" type="text"
                                               placeholder="选择时间" autocomplete="off" readonly/>
                                    </div>
                                    <input name="startTime" id="startTime" type="hidden"/>
                                    <input name="endTime" id="endTime" type="hidden"/>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">指令：</label>
                                    <div class="layui-input-inline">
                                        <input name="comand" class="layui-input" type="text" placeholder="请输入指令"
                                               autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">指令类型：</label>
                                    <div class="layui-input-inline">
                                        <select name="type" lay-search class="common-select-search">
                                            <option value="">全部</option>
                                            <option value="0">发送</option>
                                            <option value="1">收到</option>
                                            <option value="2">失败</option>
                                            <option value="3">重连</option>
                                            <option value="4">电机</option>
                                            <option value="5">远程开柜反馈</option>
                                            <option value="6">打印</option>
                                            <option value="7">异常</option>
                                            <option value="8">远程开柜</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"
                                            lay-submit layui-form-keyDownSearch="true">查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows" style="display: none">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt"></div>
                        <div class="lay-btn-rt"></div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="commandLogTable" lay-filter="commandLogTable"></table>
        </div>
    </div>
</div>

<style>
    .layui-form.model-form.commandLogDetail {
        padding: 10px;
        height: 100%;
        box-sizing: border-box;
    }

    .commandLogDetail .open-details-box {
        width: 100%;
        height: calc(100% - 50px);
        overflow-y: scroll;
    }
    .open-details-box .borderNone{
        border-width: 0;
        outline-width: 0;
    }
</style>

<script type="text/html" id="commandLogTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a>
</script>


<!-- 详情表单弹窗 -->
<script type="text/html" id="commandLogTableForm">
    <form lay-filter="commandLogTableForm" class="layui-form model-form commandLogDetail">
        <div class="open-details-box">
            <div class="layui-form-item layui-row">
                <div class="layui-col-md4">
                    <label class="layui-form-label">日志时间：</label>
                    <div class="layui-input-block">
                        <input name="createTime" class="layui-input borderNone" type="text" readonly/>
                    </div>
                </div>
                <div class="layui-col-md4">
                    <label class="layui-form-label">柜子编号：</label>
                    <div class="layui-input-block">
                        <input name="cabCode" class="layui-input borderNone" type="text" readonly/>
                    </div>
                </div>
                <div class="layui-col-md4">
                    <label class="layui-form-label">企业编号：</label>
                    <div class="layui-input-block">
                        <input name="enterpriseCode" class="layui-input borderNone" type="text" readonly/>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-col-md4">
                    <label class="layui-form-label">货道：</label>
                    <div class="layui-input-block">
                        <input name="spaceCode" class="layui-input borderNone" type="text" readonly/>
                    </div>
                </div>
                <div class="layui-col-md4">
                    <label class="layui-form-label">指令类型：</label>
                    <div class="layui-input-block">
                        <input name="typeDesc" class="layui-input borderNone" type="text" readonly/>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">指令:</label>
                <div class="layui-input-block">
                    <textarea name="command"  class="layui-textarea borderNone" readonly disabled style="padding-top: 9px;resize: none;min-height: 300px"></textarea>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'laydate'], function () {
        let $ = layui.jquery;
        let {layer, form, table, config, admin, upload, laydate} = layui;

        admin.selectSearch();

        //时间范围
        laydate.render({
            elem: '#pTime',
            type: 'datetime',
            range: true,
            done: function (value, date, endDate) {
                /*if (value) {
                    let dateValue = value.split(" - ");
                    $("#startTime").val(dateValue[0] + " 00:00:00");
                    $("#endTime").val(dateValue[1] + " 23:59:59");
                } else {
                    $("#startTime").val("");
                    $("#endTime").val("");
                }*/
                if (value) {
                    let dateValue = value.split(" - ");
                    let startTime = dateValue[0];
                    let endTime = dateValue[1];
                    // 检查用户是否选择了时分秒
                    if (!/\d{2}:\d{2}:\d{2}$/.test(startTime)) {
                        startTime += " 00:00:00"; // 默认时分秒为00:00:00
                    }

                    $("#startTime").val(startTime);
                    $("#endTime").val(validateAndReplaceTime(endTime));
                } else {
                    $("#startTime").val("");
                    $("#endTime").val("");
                }
                setTimeout(function () {
                    $('#pTime').val($("#startTime").val() + ' - ' + $("#endTime").val());
                    $('[lay-filter="formSubSearchLog"]').click();
                }, 300);
            }
        });

        function validateAndReplaceTime(time) {
            // 定义正则表达式，匹配形如 'YYYY-MM-DD 00:00:00' 的时间格式
            let regex = /^(\d{4}-\d{2}-\d{2}) 00:00:00$/;

            // 验证时间格式是否符合要求
            if (regex.test(time)) {
                // 如果时间格式符合要求，则替换时分秒为 '23:59:59'
                return time.replace(/00:00:00$/, '23:59:59');
            } else {
                // 如果时间格式不符合要求，则返回 false
                return time;
            }
        }

        // 渲染表格
        let insTb = table.render({
            elem: '#commandLogTable',
            url: config.base + 'enterprisecab/cabinet/admin/commandLog/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号'}
                , {field: 'createTime', title: '创建时间', width: 160}
                , {field: 'cabCode', title: '柜子编号', width: 250}
                , {field: 'enterpriseCode', title: '企业编号', width: 150}
                , {field: 'typeDesc', title: '指令类型', width: 120}
                , {field: 'spaceCode', title: '货道编号', width: 150}
                , {field: 'command', title: '指令'}
                , {align: 'center', fixed: 'right', toolbar: '#commandLogTableBar', title: '操作', width: 80}
            ]],
        });

        // 工具条点击事件
        table.on('tool(commandLogTable)', function (obj) {
            let data = obj.data;
            if (obj.event == 'detail') { //详情
                showDetailModel(data)
            }
        });

        //监听排序
        table.on('sort(commandLogTable)', function (obj) {
            table.reload('storageInventoryInoutTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });
        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            var field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        function showDetailModel(_data) {
            admin.open({
                type: 1,
                area: ['1200px', '700px'],
                offset: '65px',
                title: '详情',
                content: $('#commandLogTableForm').html(),
                success: function () {
                    form.val('commandLogTableForm', _data);
                }
            })
        }
    });
</script>